package com.codefish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codefish.dto.RoleDto;
import com.codefish.entity.SysRoleEntity;
import com.codefish.mapper.SysRoleDao;
import com.codefish.service.SysRoleService;
import com.codefish.util.IdWorker;
import com.codefish.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Classname SysRoleServiceImpl
 * @Description 角色表
 * @Date 2021/5/3 12:02
 * @Created 学兔斯基眯眼看世界
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {

    private static final Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存角色
     *
     * @param roleDto
     */
    @Override
    public R saveRole(RoleDto roleDto) {
        SysRoleEntity roleEntity = roleDto.getRoleEntity();
        List<Long> permissionIds = roleDto.getPermissionIds();

        permissionIds.remove(0L);

        if (roleEntity.getId() != null) { // 修改
            return this.updateRole(roleEntity, permissionIds);
        } else { //新增
            return this.saveRole(roleEntity, permissionIds);
        }

    }

    /**
     * 分页条件查询
     *
     * @param name
     * @param currentPage
     * @param limit
     * @return
     */
    @Override
    public IPage<SysRoleEntity> getPageByParams(String name, int currentPage, int limit) {
        // 分页
        Page<SysRoleEntity> page = new Page<>(new Long(currentPage), new Long(limit));

        // 构建查询条件
        QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<SysRoleEntity>().eq("is_del", "0").eq("status", "1");

        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }

        IPage<SysRoleEntity> entityIPage = baseMapper.selectPage(page, queryWrapper);

        return entityIPage;
    }

    /**
     * 根据角色主键id查询保存的菜单资源id
     *
     * @param id
     * @return
     */
    @Override
    public List<Long> queryPermissionIdsByRoleId(Long id) {
        return baseMapper.queryPermissionIdsByRoleId(id);
    }

    /**
     * 逻辑删除
     * @param id
     */
    @Override
    public void deleteRole(Long id) {

    }

    private R updateRole(SysRoleEntity roleEntity, List<Long> permissionIds) {
        SysRoleEntity r = baseMapper.getRole(roleEntity.getName());
        if (r != null && r.getId() != roleEntity.getId()) {
            return R.error(roleEntity.getName() + "已存在");
        }

        baseMapper.updateById(roleEntity);

        baseMapper.deleteRolePermission(roleEntity.getId());

        if (!CollectionUtils.isEmpty(permissionIds)) {
            baseMapper.saveRolePermission(roleEntity.getId(), permissionIds);
        }
        log.debug("修改角色{}", roleEntity.getName());
        return R.ok();
    }

    private R saveRole(SysRoleEntity role, List<Long> permissionIds) {
        SysRoleEntity r = baseMapper.getRole(role.getName());
        if (r != null) {
            return R.error(role.getName() + "已存在");
        }

        role.setId(idWorker.nextId());
        baseMapper.insert(role);
        if (!CollectionUtils.isEmpty(permissionIds)) {
            baseMapper.saveRolePermission(role.getId(), permissionIds);
        }
        log.debug("新增角色{}", role.getName());
        return R.ok();
    }
}
